<?php

namespace app\controllers;

use yii\web\Controller;

class CateController extends Controller
{
    public $menus = [
        // 一级菜单 (10个)
        ['id' => 1, 'parent_id' => 0, 'name' => '电子产品', 'level' => 1],
        ['id' => 2, 'parent_id' => 0, 'name' => '服装鞋帽', 'level' => 1],
        ['id' => 3, 'parent_id' => 0, 'name' => '家居用品', 'level' => 1],
        ['id' => 4, 'parent_id' => 0, 'name' => '食品饮料', 'level' => 1],
        ['id' => 5, 'parent_id' => 0, 'name' => '图书音像', 'level' => 1],
        ['id' => 6, 'parent_id' => 0, 'name' => '运动户外', 'level' => 1],
        ['id' => 7, 'parent_id' => 0, 'name' => '美妆护肤', 'level' => 1],
        ['id' => 8, 'parent_id' => 0, 'name' => '母婴用品', 'level' => 1],
        ['id' => 9, 'parent_id' => 0, 'name' => '汽车用品', 'level' => 1],
        ['id' => 10, 'parent_id' => 0, 'name' => '数码配件', 'level' => 1],

        // 二级菜单 (20个)
        ['id' => 11, 'parent_id' => 1, 'name' => '手机通讯', 'level' => 2],
        ['id' => 12, 'parent_id' => 1, 'name' => '电脑办公', 'level' => 2],
        ['id' => 13, 'parent_id' => 2, 'name' => '男装', 'level' => 2],
        ['id' => 14, 'parent_id' => 2, 'name' => '女装', 'level' => 2],
        ['id' => 15, 'parent_id' => 3, 'name' => '厨房用品', 'level' => 2],
        ['id' => 16, 'parent_id' => 3, 'name' => '卧室用品', 'level' => 2],
        ['id' => 17, 'parent_id' => 4, 'name' => '零食小吃', 'level' => 2],
        ['id' => 18, 'parent_id' => 4, 'name' => '饮料冲调', 'level' => 2],
        ['id' => 19, 'parent_id' => 5, 'name' => '教育图书', 'level' => 2],
        ['id' => 20, 'parent_id' => 5, 'name' => '文学小说', 'level' => 2],
        ['id' => 21, 'parent_id' => 6, 'name' => '运动服装', 'level' => 2],
        ['id' => 22, 'parent_id' => 6, 'name' => '运动器材', 'level' => 2],
        ['id' => 23, 'parent_id' => 7, 'name' => '面部护理', 'level' => 2],
        ['id' => 24, 'parent_id' => 7, 'name' => '彩妆香水', 'level' => 2],
        ['id' => 25, 'parent_id' => 8, 'name' => '奶粉辅食', 'level' => 2],
        ['id' => 26, 'parent_id' => 8, 'name' => '玩具用品', 'level' => 2],
        ['id' => 27, 'parent_id' => 9, 'name' => '汽车配件', 'level' => 2],
        ['id' => 28, 'parent_id' => 9, 'name' => '汽车装饰', 'level' => 2],
        ['id' => 29, 'parent_id' => 10, 'name' => '手机配件', 'level' => 2],
        ['id' => 30, 'parent_id' => 10, 'name' => '电脑配件', 'level' => 2],

        // 三级菜单 (20个)
        ['id' => 31, 'parent_id' => 11, 'name' => '智能手机', 'level' => 3],
        ['id' => 32, 'parent_id' => 11, 'name' => '功能手机', 'level' => 3],
        ['id' => 33, 'parent_id' => 12, 'name' => '笔记本电脑', 'level' => 3],
        ['id' => 34, 'parent_id' => 12, 'name' => '台式电脑', 'level' => 3],
        ['id' => 35, 'parent_id' => 13, 'name' => 'T恤衬衫', 'level' => 3],
        ['id' => 36, 'parent_id' => 13, 'name' => '裤子外套', 'level' => 3],
        ['id' => 37, 'parent_id' => 14, 'name' => '连衣裙', 'level' => 3],
        ['id' => 38, 'parent_id' => 14, 'name' => '上衣下装', 'level' => 3],
        ['id' => 39, 'parent_id' => 15, 'name' => '锅具厨具', 'level' => 3],
        ['id' => 40, 'parent_id' => 15, 'name' => '餐具用品', 'level' => 3],
        ['id' => 41, 'parent_id' => 16, 'name' => '床上用品', 'level' => 3],
        ['id' => 42, 'parent_id' => 16, 'name' => '家具装饰', 'level' => 3],
        ['id' => 43, 'parent_id' => 17, 'name' => '膨化食品', 'level' => 3],
        ['id' => 44, 'parent_id' => 17, 'name' => '坚果炒货', 'level' => 3],
        ['id' => 45, 'parent_id' => 18, 'name' => '碳酸饮料', 'level' => 3],
        ['id' => 46, 'parent_id' => 18, 'name' => '茶饮咖啡', 'level' => 3],
        ['id' => 47, 'parent_id' => 19, 'name' => '教材教辅', 'level' => 3],
        ['id' => 48, 'parent_id' => 19, 'name' => '考试用书', 'level' => 3],
        ['id' => 49, 'parent_id' => 20, 'name' => '现代文学', 'level' => 3],
        ['id' => 50, 'parent_id' => 20, 'name' => '古典文学', 'level' => 3],
    ];

    public function actionIndex()
    {
        // 生成50条三级树形结构数据
        $menus = $this->menus;

// 测试 getTreeChildrenById 方法
        function testGetTreeChildrenById($menus, $id) {
            echo "=== 获取ID为 {$id} 的树形结构 ===\n";

            // 模拟 CUtil::getTreeChildrenById 方法
            $result = getTreeChildrenById($menus, $id);

            echo "结果：\n";
            print_r($result);
            echo "\n";
        }

        //获取树形结构 使用&符号实现引用传递，避免重复数据
        function getTreeChildrenById($menus, $id, $idField = 'id',$pidField = 'parent_id', $childrenField = 'children') {
            // 1. 获取所有数据
            if (empty($menus)) {
                return [];
            }

            // 2. 将数组转为以ID为键的关联数组，方便查询
            $dataMap = [];
            foreach ($menus as $menu) {
                $dataMap[$menu[$idField]] = $menu;
                $dataMap[$menu[$idField]][$childrenField] = [];
            }

            // 3. 检查目标是否存在
            if (!isset($dataMap[$id])) {
                return [];
            }

            // 4. 构建树形结构
            foreach ($menus as $menu) {
                $pid = $menu[$pidField];
                // 如果存在父级，就添加到父级的children中
                if (!empty($pid) && isset($dataMap[$pid])) {
                    $dataMap[$pid][$childrenField][] = &$dataMap[$menu[$idField]];
                }
            }

            // 5. 返回指定层级及所有子级
            return $dataMap[$id];
        }

// 测试不同层级的树形结构
        echo "=== 三级树形结构测试数据 (50条) ===\n";
        echo "数据结构：\n";
        echo "- 一级菜单：10个 (ID: 1-10)\n";
        echo "- 二级菜单：20个 (ID: 11-30)\n";
        echo "- 三级菜单：20个 (ID: 31-50)\n\n";

// 测试获取一级菜单的完整树形结构
        testGetTreeChildrenById($menus, 1);  // 电子产品及其所有子级

// 测试获取二级菜单的树形结构
        testGetTreeChildrenById($menus, 11); // 手机通讯及其子级

// 测试获取三级菜单（叶子节点）
        testGetTreeChildrenById($menus, 31); // 智能手机（无子级）

// 输出完整数据
        echo "=== 完整数据数组 ===\n";
        echo "<?php\n";
        echo "\$menus = " . var_export($menus, true) . ";\n";
        echo "?>\n";
    }

    public function actionGetPath()
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $data[] = $this->getPath(44);
        $data[] = $this->getPath2(44);
        $data[] = $this->getParentPath2(44);
        $data[] = $this->getParentPathStr2(44);
        return $data;
    }

    public function getPath($id, $all = [])
    {
        if (empty($all)) {
            $all = $this->menus;
            $all = array_column($all, null,'id');
        }
        $info = $all[$id] ?? [];
        if (empty($info)) { return [];}
        $currentId = $id;
        $path = [];
        while ($currentId != 0) {
            if (empty($all[$currentId])) {
                break;
            }
            $path[] = $currentId;
            $currentId = $all[$currentId]['parent_id'];
        }
        // 反转路径，使其从根节点开始
        return array_reverse($path);
    }

    public function getPath2($id, $pidName = 'parent_id')
    {
        $all = $this->menus;
        $all = array_column($all, null,'id');
        if (($info = ($all[$id] ?? null))) {
            $pid = $info[$pidName];
            $parentPath = $this->{__FUNCTION__}($pid);
            return array_merge($parentPath, [$id]);
        } else {
            return [];
        }
    }

    public function getParentPath2($id,  $pidName = 'parent_id')
    {
        $all = $this->menus;
        $all = array_column($all, null,'id');
        if ( ($info = ($all[$id] ?? null)) && $info[$pidName] > 0 ) {
            $pid = $info[$pidName];
            $parentPath = $this->{__FUNCTION__}($pid);
            return array_merge($parentPath, [$pid]);
        } else {
            return [];
        }
    }

    public function getParentPathStr2($id, $pidName = 'parent_id')
    {
        $all = $this->menus;
        $all = array_column($all, null,'id');
        $info = $all[$id];
        $pid = $info[$pidName];
        if ($pid > 1) {
            $parentPath = $this->{__FUNCTION__}($pid);
            return $parentPath . ',' . $pid;
        } else {
            return '';
        }
    }
}